package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    /**
     * 根据用户名 查询自媒体用户
     *
     * @param name
     * @return
     */
    @Override
    public WmUser findByName(String name) {
        //1.判断参数是否为空
        if (StringUtils.isBlank(name)) {
            return null;
        }
        // LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery();
        // wrapper.eq(WmUser::getName, name);
        //2.查询自媒体用户
        WmUser wmUser = getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return wmUser;
    }

    /**
     * 保存自媒体用户
     *
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult insert(WmUser wmUser) {
        //1.判断参数是否为空
        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存自媒体用户
        save(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 自媒体用户登陆
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto dto) {
        //1.判断参数是否为空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.判断用户名和密码是否为空(一个为空都不行)
        if (StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //3.根据用户名查询用户判断是否存在

        // LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq();

        // LambdaQueryWrapper<AdUser> wrapper = Wrappers.<AdUser>lambdaQuery();

        LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, dto.getName());
        WmUser wmUser = getOne(wrapper);

        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }

        //4.利用MD5+salt 加密方式登录密码,与表中的密码作比较
        String loginPwd = dto.getPassword(); //用户输入的明文密码
        String salt = wmUser.getSalt(); //随机数（盐）
        //将明文密码与盐拼接
        String pwd = loginPwd + salt;

        //对拼接的密码进行MD5加密
        String encryptPwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        //数据库表中该用户的密文密码
        String pwdDB = wmUser.getPassword();

        //5.比较密码
        if (!encryptPwd.equals(pwdDB)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //如果比较成功
        String token = AppJwtUtil.getToken(wmUser.getId().longValue());

        Map userMap = new HashMap<>();
        userMap.put("token", token);

        //不要把敏感信息返回给前端,设置为空
        wmUser.setPassword("");
        wmUser.setSalt("");

        userMap.put("user", wmUser);

        //6.返回数据
        return ResponseResult.okResult(userMap);
    }
}
